//
//  BaseViewController.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/4/19.
//

#import "BaseViewController.h"

@interface BaseViewController ()

@end

@implementation BaseViewController

@synthesize customNavigationBar = _customNavigationBar;

- (JPNavigationBar *)customNavigationBar{
    if (!_customNavigationBar) {
        _customNavigationBar = [[JPNavigationBar alloc] init];
        _customNavigationBar.backgroundColor = KbgColor;
    }
    return _customNavigationBar;
}



- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    [self.view bringSubviewToFront:self.customNavigationBar];
}

- (void)setTitle:(NSString *)title{
    self.customNavigationBar.title = title;
}

- (NSString *)title{
    if ([self.customNavigationBar.titleView isKindOfClass:[UILabel class]]){
        return ((UILabel *)self.customNavigationBar.titleView).text;
    }
    return nil;
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
   
}


-(void)viewDidLoad{
    [super viewDidLoad];
    
    self.currentAvailableWidth = [DeviceUtils currentAvailableWidth];
    self.currentAvailableHeight = [DeviceUtils currentAvailableHeight];
    
    //界面初始化
    [self createNav];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(needLogin) name:@"kJPUserTokenIllegalNotification" object:nil];
    
    
}

#pragma mark - 登录过期
-(void)needLogin{
    [self.navigationController popToRootViewControllerAnimated:YES];
}


#pragma mark - 界面初始化
-(void)createNav{
    self.view.backgroundColor = KbgColor;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self.view addSubview:self.customNavigationBar];
    self.customNavigationBar.backgroundColor = KbgColor;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    if (isIPAD) {
        return UIInterfaceOrientationMaskAll; // iPad 支持所有方向
    } else {
        return UIInterfaceOrientationMaskPortrait; // 只支持竖屏
    }
}



@end

@implementation BaseViewController (Utils)

#pragma mark - 获取手机当前显示的ViewController
-(UIViewController*)currentViewController{
    UIWindow *keyWindow = KEY_WINDOW;
    if(keyWindow){
        UIViewController* vc = keyWindow.rootViewController;
        while (1) {
            if ([vc isKindOfClass:[UITabBarController class]]) {
                vc = ((UITabBarController*)vc).selectedViewController;
            }
            if ([vc isKindOfClass:[UINavigationController class]]) {
                vc = ((UINavigationController*)vc).visibleViewController;
            }
            if (vc.presentedViewController) {
                vc = vc.presentedViewController;
            }else{
                break;
            }
        }
        return vc;
    }
    return nil;
}

- (void)setCustomNavigationBarHidden:(BOOL)customNavigationBarHidden{
    self.customNavigationBar.hidden = customNavigationBarHidden;
}

- (BOOL)isCustomNavigationBarHidden{
    return self.customNavigationBar.isHidden;
}

- (void)defaultBackSet{
    [self.customNavigationBar setLeftItemWithIcon:[UIImage imageNamed:@"nav_back_logo"]];
    [self.customNavigationBar.leftButton addTarget:self action:@selector(__backButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)dischargeLeftOfNavBar{
    [self.customNavigationBar.leftButton removeFromSuperview];
    self.customNavigationBar.leftButton = nil;
}

- (void)__backButtonClicked:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}


-(CGFloat)getStringHeightWithText:(NSString *)text font:(UIFont *)font viewWidth:(CGFloat)width{
    // 设置文字属性 要和label的一致
    NSDictionary *attrs = @{NSFontAttributeName :font};
    CGSize maxSize = CGSizeMake(width, MAXFLOAT);
    
    NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
    
    // 计算文字占据的宽高
    CGSize size = [text boundingRectWithSize:maxSize options:options attributes:attrs context:nil].size;
    
    // 当你是把获得的高度来布局控件的View的高度的时候.size转化为ceilf(size.height)。
    return  ceilf(size.height);
}

-(CGFloat)getStringWidthWithText:(NSString *)text font:(UIFont *)font viewHeight:(CGFloat)height{
    // 设置文字属性 要和label的一致
    NSDictionary *attrs = @{NSFontAttributeName :font};
    CGSize maxSize = CGSizeMake(MAXFLOAT, height);
    
    NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
    
    // 计算文字占据的宽高
    CGSize size = [text boundingRectWithSize:maxSize options:options attributes:attrs context:nil].size;
    
    // 当你是把获得的高度来布局控件的View的高度的时候.size转化为ceilf(size.height)。
    return  ceilf(size.width);
}


-(void)popGestureClose{
    // 禁用侧滑返回手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        //这里对添加到右滑视图上的所有手势禁用
        for (UIGestureRecognizer *popGesture in self.navigationController.interactivePopGestureRecognizer.view.gestureRecognizers) {
            popGesture.enabled = NO;
        }
    }
}

-(void)popGestureOpen{
    // 启用侧滑返回手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        //这里对添加到右滑视图上的所有手势启用
        for (UIGestureRecognizer *popGesture in self.navigationController.interactivePopGestureRecognizer.view.gestureRecognizers) {
            popGesture.enabled = YES;
        }
    }
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

@end
